package com.uwo.house.controller;

import com.uwo.house.beans.User;
import com.uwo.house.beans.request.MeasureCenterReq;
import com.uwo.house.beans.request.MeasureCenterSurveyReq;
import com.uwo.house.beans.response.MeasureOrderResp;
import com.uwo.house.config.JWTAuthenticationToken;
import com.uwo.house.kits.CacheKits;
import com.uwo.house.service.IMeasureCenterService;
import com.uwo.house.service.IOrderMeasureService;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by PHS on 2017/7/19.
 * 测量中心Controller
 */
@Controller
@RequestMapping("/measureCenter")
public class MeasureCenterController extends BaseController{
    @Autowired
    private IMeasureCenterService measureCenterService;

    @Autowired
    private IOrderMeasureService orderMeasureService;

    /**
     * 测量中心显示订单列表
     * @param orderId
     * @param orderDateFromStr
     * @param orderDateToStr
     * @param orderSts
     * @return
     */
    @RequestMapping(value = "/measureOrderList", method = RequestMethod.GET)
    @ResponseBody
    public Map measureOrderList(@RequestParam(value = "orderId", required = false) String orderId,
                                  @RequestParam(value = "orderDateFromStr", required = false) String orderDateFromStr,
                                  @RequestParam(value = "orderDateToStr", required = false) String orderDateToStr,
                                  @RequestParam(value = "orderSts",defaultValue="0") int orderSts) {
        Date orderDateFrom = com.uwo.house.util.DateUtils.getDate(orderDateFromStr);
        Date orderDateTo = com.uwo.house.util.DateUtils.getDate(orderDateToStr);
        List<MeasureOrderResp> measureOrderList = measureCenterService.getMeasureOrderTo(orderId, orderDateFrom, orderDateTo, orderSts);
        Map map = UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, measureOrderList);
        return map;
    }


    /**
     * 测量人员接单接口
     * @param orderId
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/receiveOrder" ,method = RequestMethod.POST)
    public Map receiveOrder(@RequestParam(value = "orderId") String orderId,
                            @RequestParam(value = "userId") String userId){
        int result = measureCenterService.receiveOrder(orderId,userId);
        if (result == 1){
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "接单成功！");
        } else {
            return UtilReturn.resErrorMap("接单失败",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 测量人员进入上传页面  同时查询订单的详细信息
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/toUploadPage", method = RequestMethod.POST)
    @ResponseBody
    public Map toUploadPage(@RequestParam("orderId") String orderId) {
        MeasureOrderResp measureOrderResp = orderMeasureService.getMeasureDetailsByOrderId(orderId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, measureOrderResp);
    }

    /**
     * 测量人员上传测量信息资料接口  同时更新订单状态
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadInfo" , method = RequestMethod.POST)
    public Map uploadInfo(@RequestParam(value = "orderId") String orderId,
                          @RequestParam(value = "mobile") String mobile,
                          @RequestParam(value = "measureType") String measureType,
                          @RequestParam(value = "photoUrl") String photo,
                          @RequestParam(value = "videoUrl") String video,
                          @RequestParam(value = "constructionArea") Integer constructionArea,
                          @RequestParam(value = "insideArea") Integer insideArea,
                          @RequestParam(value = "measureTimeStr") String measureTimeStr,
                          @RequestParam(value = "userId") String userId,
                          @RequestBody MeasureCenterSurveyReq measureCenterSurveyReq) {
        MeasureCenterReq measureCenterReq = new MeasureCenterReq();
        Date measureTime = com.uwo.house.util.DateUtils.getDate(measureTimeStr);
        measureCenterReq.setContractNumber(orderId);
        measureCenterReq.setMobile(mobile);
        measureCenterReq.setMeasureTime(measureTime);
        measureCenterReq.setMeasureType(measureType);
        measureCenterReq.setPhoto(photo);
        measureCenterReq.setVideo(video);
        measureCenterReq.setMeasureUserId(userId);
        measureCenterReq.setConstructionArea(constructionArea);
        measureCenterReq.setInsideArea(insideArea);
        measureCenterReq.setDelFlag(0);
        measureCenterReq.setRewardStatus(0);
        measureCenterReq.setRewardMoney(5);
        int result = measureCenterService.addMeasureInfoAndSurvey(measureCenterReq,measureCenterSurveyReq);
        MeasureOrderResp measureOrderResp = orderMeasureService.getMeasureDetailsByOrderId(orderId);
        String yUserId = "";
        if (null != measureOrderResp) {
            yUserId = measureOrderResp.getUserId();
        }
        if (result == 1){
            this.addRecord("我的新房测量完成",yUserId,photo,video);
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "上传成功！");
        } else {
            return UtilReturn.resErrorMap("上传失败",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 测量人员删除完成订单
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/deleteMeasureOrder", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteMeasureOrder(@RequestParam(value = "orderId") String orderId) {
        orderMeasureService.deleteMeasureOrder(orderId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
    }

    /**
     * 根据 userId 查询奖励列表接口
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/rewardList" ,method = RequestMethod.POST)
    public Map rewardList(@RequestHeader(value = "Authorization") String token){
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        User user = jwtToken.getUser();
        String userId = user.getId();
        List<MeasureCenterReq> rewardList = measureCenterService.rewardList(userId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, rewardList);
    }

    /**
     * 测量人员领取奖励接口
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/receiveReward" ,method = RequestMethod.POST)
    public Map receiveReward(@RequestParam(value = "id") Integer id){
        MeasureCenterReq measureCenterReq = new MeasureCenterReq();
        measureCenterReq.setId(id);
        measureCenterReq.setRewardStatus(1);
        int result = measureCenterService.reward(measureCenterReq);
        if (result == 1){
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "领奖成功！");
        } else {
            return UtilReturn.resErrorMap("领奖失败!",UtilReturn.ERROR_CODE);
        }
    }



}
